package com.example.controller;

import com.example.pojo.User;
import com.example.service.UserService;
import com.example.utils.ReadExcelUtil;
import com.example.utils.Result;
import com.example.pojo.Student;
import com.example.service.StudentService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@Api(value = "学生管理接口")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private UserService userService;
    //查询
    @GetMapping("/students")
    public Result list(){
        log.info("查询学生列表");
        List<Student> list = studentService.list();
        return Result.success(list);
    }
    //根据id查询
    @GetMapping("/students/{id}")
    public Result query(@PathVariable Integer id){
        log.info("查询学生{}："+id);
        Student student = studentService.query(id);
        return Result.success(student);
    }
    //添加
    @PostMapping("/Students")
    public Result add(@RequestBody Student student){
        log.info("成功添加学生{}："+student);
        //关联user表
        try {
            User user = userService.query(student.getStuName());
            student.setLoginId(user.getId());
        }catch (Exception e){
            return Result.error("没有此用户");
        }

        studentService.add(student);
        return Result.success();
    }
    //修改
    @PutMapping("/students")
    public Result update(@RequestBody Student student){
        log.info("成功修改{}信息"+student);
        studentService.update(student);
        return Result.success();
    }
    //审核学员
    @PutMapping("/students/{id}")
    public Result check(@PathVariable Integer id){
        log.info("审核学员{}成功"+id);
        studentService.approve(id);
        return Result.success();
    }
    //批量审核学员
    @PutMapping("/students/approve")
    public Result checkAll(@RequestParam("ids") List<Integer> ids){
        log.info("批量审核成功");
        studentService.batchApprove(ids);
        return Result.success();
    }

    //批量导入学生
    @PostMapping("/students/import")
    public Result importStudent() throws Exception {
        log.info("批量添加学生");
        String path = "C:\\Users\\lemon\\Desktop\\";
        // 1.获取文件流
        FileInputStream fileInputStream = new FileInputStream(path + "student.xlsx");
        try{
            // 2.创建一个工作簿。使用excel能操作的这边他也可以操作。
            Workbook workbook = new XSSFWorkbook(fileInputStream);
            //调用工具类
            List<Student> list1 = ReadExcelUtil.readExcel(workbook,fileInputStream,Student.class);
            System.out.println(list1);
            studentService.batchImport(list1);
        }catch (Exception e){
            return Result.error("添加失败");
        }

        return Result.success();

    }


}
